package com.yunquan.ohana.ui.mine.dialog

import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.Window
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDialog
import androidx.databinding.DataBindingUtil
import com.yunquan.ohana.R
import com.yunquan.ohana.databinding.DialogContactsBinding
import com.yunquan.ohana.utils.ToastUtils

class ContactsDialog(private val context: Context) : AppCompatDialog(context) {

    private lateinit var binding: DialogContactsBinding
    private var mEmail: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setCanceledOnTouchOutside(false)
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_contacts, null, false)
        setContentView(binding.root)
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setGravity(Gravity.BOTTOM)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        initView()
    }

    @SuppressLint("SetTextI18n")
    private fun initView() {
        binding.tvEmail.text = "Email: $mEmail"
        binding.btnCancel.setOnClickListener { dismiss() }
        binding.tvEmail.setOnClickListener {
            copyToClipboard()
            dismiss()
        }
    }

    fun setContacts(txt: String?) {
        mEmail = txt
    }


    private fun copyToClipboard() {
        val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clip = ClipData.newPlainText("Email", mEmail!!)
        clipboard.setPrimaryClip(clip)
        ToastUtils.showShort("Copy Success")
    }
}